//
//  ViewController.m
//  avAudioPlayer
//
//  Created by 伍银荣 on 2017/6/1.
//  Copyright © 2017年 WYR. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<UIWebViewDelegate>

{
    __weak IBOutlet UIWebView *webView;
    __weak IBOutlet UIActivityIndicatorView *activity;
}
@property(nonatomic,strong)AVAudioPlayer *avPlayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadWebView];
    [activity setHidesWhenStopped:YES];
    [activity sizeToFit];
    [activity startAnimating];
    
    //网络获取MP3
    NSString *strUrl = @"http://sc1.111ttt.com/2017/1/05/09/298092042172.mp3";
    NSURL *url = [NSURL URLWithString:strUrl];
    //将url转化成data类型
    NSData *data = [NSData dataWithContentsOfURL:url];
    //保存数据
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    //设置一个文件名
    NSString *filePath = [NSString stringWithFormat:@"%@/%@.mp3",path,@"music"];
    //保存（写入）
    [data writeToFile:filePath atomically:YES];
    NSLog(@"filePath:%@",filePath);
    [self loadMP3:filePath];
    
}
//播放影音
-(void)loadMP3:(NSString *)mp3Str
{
    _avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:mp3Str] error:nil];
    _avPlayer.volume = 1.0f;
    [_avPlayer prepareToPlay];
    [_avPlayer play];
    
}
//加载webView
-(void)loadWebView
{
    webView.delegate = self;
    //将活动指示器在视图显示前显示
    [self.view bringSubviewToFront:activity];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
    [webView loadRequest:request];
    
}

//前进
- (IBAction)forward:(id)sender {
    [webView goForward];
}

//刷新
- (IBAction)reload:(id)sender {
    [webView reload];
}
//后退
- (IBAction)back:(id)sender {
    [webView goBack];
}

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [activity stopAnimating];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
